package com.example.demo.controller;


import com.example.demo.entity.StudentExam;
import com.example.demo.service.IStudentExamService;
import com.example.demo.utils.AjaxJson;
import com.example.demo.vo.studentQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生考试记录表 前端控制器
 * </p>
 *
 * @author 
 * @since 2025-11-10
 */
@RestController
@RequestMapping("/student-exam")
public class StudentExamController {
    
    @Autowired
    private IStudentExamService studentExamService;
    
    @RequestMapping("/add")
    public AjaxJson add(@RequestBody StudentExam studentExam) {
        return studentExamService.save(studentExam) ? AjaxJson.getSuccess("添加成功") : AjaxJson.getError("添加失败");
    }
    
    @RequestMapping("/delete")
    public AjaxJson delete(@RequestParam Integer id) {
        return studentExamService.removeById(id) ? AjaxJson.getSuccess("删除成功") : AjaxJson.getError("删除失败");
    }
    
    @RequestMapping("/update")
    public AjaxJson update(@RequestBody StudentExam studentExam) {
        return studentExamService.updateById(studentExam) ? AjaxJson.getSuccess("修改成功") : AjaxJson.getError("修改失败");
    }
    
    @RequestMapping("/query")
    public AjaxJson query() {
        return AjaxJson.getPageData(studentExamService.count(), studentExamService.list());
    }
    
    @RequestMapping("/query/id")
    public AjaxJson query(@RequestParam Integer id) {
        return AjaxJson.getSuccessData(studentExamService.getById(id));
    }

    /**
     * 保存答题进度
     * @param studentExam
     * @return
     */
    @PostMapping("/save/student")
    public AjaxJson saveStudent(@RequestBody studentQuestionVo studentExam) {
        return studentExamService.saveStudent(studentExam);
    }

    /**
     * 查询答题进度
     * @return
     */
    @GetMapping("/query/student")
    public AjaxJson queryStudent(@RequestParam Integer examId) {
        return studentExamService.queryStudent(examId);
    }

    /**
     * 记录考生进入考试
     */
    @PostMapping("/record/enter")
    public AjaxJson recordEnter(@RequestParam Integer examId) {
        return studentExamService.recordEnter(examId);
    }

    /**
     * 提交考试
     */
    @PostMapping("/submit")
    public AjaxJson submit(@RequestBody studentQuestionVo studentExam) {
        // TODO 后台进行自动打分
        return studentExamService.submit(studentExam);
    }

    /**
     * 考生查看考试结果
     */
    @GetMapping("/query/result")
    public AjaxJson queryResult(@RequestParam Integer examId) {
        return studentExamService.queryResult(examId);
    }
}